package com.example.barry.pdf.Utils;

import android.os.Handler;
import android.os.Message;
import android.util.Log;


import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpResponseException;
import org.ksoap2.transport.HttpTransportSE;
import org.xmlpull.v1.XmlPullParserException;

import java.io.IOException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * Created by barry on 17-1-18.
 */

public class WebServiceUtils {
    public static final String WEB_SERVER_URL = "http://172.26.100.230:8080/sfdc-webservice/services/SFDCService";

    // 含有3个线程的线程池
    private static final ExecutorService executorService = Executors
            .newFixedThreadPool(3);

    // 命名空间
    private static final String NAMESPACE = "http://172.26.100.230/sfdc-webservice/services/SFDCService";

    /**
     *
     * @param url
     *            WebService服务器地址
     * @param methodName
     *            WebService的调用方法名
     * @param properties
     *            WebService的参数
     * @param webServiceCallBack
     *            回调接口
     */
    public static void callSendAutoSFDCService(String url, final String methodName,
                                      Object[] properties,
                                      final WebServiceCallBack webServiceCallBack) {
        // 创建HttpTransportSE对象，传递WebService服务器地址
        final HttpTransportSE httpTransportSE = new HttpTransportSE(url);
        // 创建SoapObject对象
        SoapObject soapObject = new SoapObject(NAMESPACE, methodName);
    Log.i("url",url);
        // SoapObject添加参数
        if (methodName.equals("sendAutoSFDC")) {
           /* for (Iterator<Map.Entry<String, String>> it = properties.entrySet()
                    .iterator(); it.hasNext();) {
                Map.Entry<String, String> entry = it.next();
                soapObject.addProperty(entry.getKey(), entry.getValue());
            }*/
            soapObject.addProperty("data",properties[0]);
            soapObject.addProperty("deviceID",properties[1]);
            soapObject.addProperty("serverIP",properties[2]);
            soapObject.addProperty("port",properties[3]);
            soapObject.addProperty("includeErrorCode",properties[4]);
        }if (methodName.equals("querySerialInfo")){
            Log.i("F",methodName);
            soapObject.addProperty("serialNumber",properties[0]);
            soapObject.addProperty("deviceId",properties[1]);
            Log.i("serialNumber",properties[0]+"");
            Log.i("deviceid",properties[1]+"");
        }

        // 实例化SoapSerializationEnvelope，传入WebService的SOAP协议的版本号
        final SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(
                SoapEnvelope.VER11);
        // 设置是否调用的是.Net开发的WebService
        soapEnvelope.setOutputSoapObject(soapObject);
        soapEnvelope.dotNet = true;
        httpTransportSE.debug = true;

        // 用于子线程与主线程通信的Handler
        final Handler mHandler = new Handler() {

            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                // 将返回值回调到callBack的参数中
                webServiceCallBack.callBack((SoapObject) msg.obj);
            }

        };

        // 开启线程去访问WebService
        executorService.submit(new Runnable() {

            @Override
            public void run() {
                SoapObject resultSoapObject = null;
                try {
                    httpTransportSE.call(NAMESPACE + methodName, soapEnvelope);
                    Log.i("连接的是",""+NAMESPACE + methodName);
                    if (soapEnvelope.getResponse() != null) {
                        // 获取服务器响应返回的SoapObject
                        resultSoapObject = (SoapObject) soapEnvelope.bodyIn;
                        String string = resultSoapObject.toString();
                        Log.i("webservice",""+string);
                        mHandler.sendMessage(mHandler.obtainMessage(0,
                                resultSoapObject));
                    }else {
                        Log.i("11","22");
                      //  mHandler.sendMessage(mHandler.obtainMessage(0,""));
                    }
                } catch (HttpResponseException e) {
                    Log.i("HttpResponseException",e.getMessage());
                } catch (IOException e) {
                    Log.i("IOException",e.getMessage());
                } catch (XmlPullParserException e) {
                    Log.i("XmlPullParserException",e.getMessage());
                } catch (Exception e){
                    Log.i("异常",e.getMessage());
                } /*finally{
                    // 将获取的消息利用Handler发送到主线程

                }*/
            }
        });
    }


    public interface WebServiceCallBack {
        public void callBack(SoapObject result);

    }
}
